package com.example.io;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @author readpage
 * @create 2022-10-17 19:40
 *
 * RandomAccessFile的使用
 * 1.RandomAccessFile直接继承于java.lang.Object类，实现了DataInput和DataOutput接口
 * 2.RandomAccessFile既可以作为一个输入流，又可以作为一个输出流
 *
 * 3.如果RandomAccessFile作为输出流时，写出到的文件如果不存在，则在执行过程中自动创建
 * 如果写出到的文件存在，则会对原有文件内容进行覆盖。(默认情况下，从头覆盖)
 */
public class RandomAccessFileTest {
    @Test
    public void test() {
        RandomAccessFile raf = null;
        RandomAccessFile raf2 = null;
        try {
            raf = new RandomAccessFile("avatar.webp", "r");
            raf2 = new RandomAccessFile("avatar5.webp", "rw");

            byte[] buffer = new byte[1024];
            int len;
            while ((len = raf.read(buffer)) != -1) {
                raf2.write(buffer, 0, len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (raf != null) {
                try {
                    raf.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (raf != null) {
                try {
                    raf2.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Test
    public void test2() {
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile("test.txt", "rw");
            raf.seek(3);  //将指针调到角标为3的位置
            raf.write("xyz".getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (raf != null) {
                try {
                    raf.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    /**
     * 使用RandomAccessFile实现数据的插入效果
     */
    @Test
    public void test3() {
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile("test.txt", "rw");
            raf.seek(3); //将指针调到角标为3的位置
            //保存指针3后面的所有数据到StringBuilder中
            StringBuilder builder = new StringBuilder((int)new File("test.txt").length());
            byte[] buffer = new byte[20];
            int len;
            while ((len = raf.read(buffer)) != -1) {
                builder.append(new String(buffer, 0, len));
            }

            //调回指针，写入"xyz"
            raf.seek(3);
            raf.write("xyz".getBytes());

            //将StringBuilder中的数据写入到文件中。
            raf.write(builder.toString().getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (raf != null) {
                try {
                    raf.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
